﻿using Newtonsoft.Json;
using System.IO;
using System.Text;

namespace MoListv1
{
    class Config
    {
        [JsonProperty("ldll")]
        public int LogDanmakuLengthLimit { get; set; } = 20;

        [JsonProperty("dmts")]
        public uint MaxTotalItemNum { get; set; } = 10;

        [JsonProperty("dmps")]
        public uint MaxTotalPersonNum { get; set; } = 2;

        [JsonProperty("up")]
        public bool IsUserPrior { get; set; } = true;

        [JsonProperty("lrd")]
        public bool IsLogRedirectDanmaku { get; set; } = false;

        [JsonProperty("now")]
        public int NowUserList { get; set; } = 0;

        [JsonProperty("blst")]
        public BlackListItem[] Blacklist { get; set; } = new BlackListItem[0];

        [JsonProperty("usst")]
        public UserListItem[] UserList { get; set; } = new UserListItem[0];

        public Config()
        {
            if (!File.Exists(Utilities.ConfigFilePath))
            {
                Write(this);
            }
        }

        internal static Config Load(bool reset = false)
        {
            Config config = new Config();
            if (!reset)
            {
                string str = File.ReadAllText(Utilities.ConfigFilePath, Encoding.UTF8);
                config = JsonConvert.DeserializeObject<Config>(str);
            }
            return config;
        }

        internal static void Write(Config config)
        {
            File.WriteAllText(Utilities.ConfigFilePath, JsonConvert.SerializeObject(config), Encoding.UTF8);
        }
    }
}
